package com.iwish.server.model;

import java.io.Serializable;

/**
 * 报文结构
 * + 0------------7------------15----------23----------31 +
 * |    Version   | RequestCode |  SubType  |  ReplyInfo  |
 * |                 Data Length                          |
 * |                    data                              |
 * 
 * @author Luoaaang
 *
 */

public class MessagePacket implements PacketTemplate,Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final byte PACKET_HEADER_LEN = 8;

	private byte version;
	private byte requestCode;
	private byte replyInfo;
	private byte subType;
	private int data_length;
	//private String cookie;
	protected byte[] data = new byte[0];
	
	public byte[] getHeader()
	{
		byte[] buf = new byte[PACKET_HEADER_LEN];
		buf[0] = this.version;
		buf[1] = this.requestCode;
		buf[2] = this.subType;
		buf[3] = this.replyInfo;
		buf[4] = (byte) (0x00FF & data_length >>> 24);
		
		int len = data_length;
		for(int i = 7; i > 3; i--)
		{
			buf[i] = (byte) (0x00FF & len);
			len >>= 8;
		}
		
		return buf;
	}
	
	public byte[] getBytes()
	{
		byte[] header = getHeader();
		byte[] data = getData();
		byte[] buf = new byte[header.length + data.length];
		
		System.arraycopy(header, 0, buf, 0, header.length);
		System.arraycopy(data, 0, buf, header.length, data.length);
		return buf;
	}
	
	public byte[] getData()
	{
		return data;
	}
	
	public void setData(byte[] data)
	{
		this.data = data;
	}
	
	//getter and setter
	public byte getVersion() {
		return version;
	}

	public void setVersion(byte version) {
		this.version = version;
	}

	public byte getRequestCode() {
		return requestCode;
	}

	public void setRequestCode(byte requestCode) {
		this.requestCode = requestCode;
	}

	public byte getReplyInfo() {
		return replyInfo;
	}

	public void setReplyInfo(byte replyInfo) {
		this.replyInfo = replyInfo;
	}

	public byte getSubType() {
		return subType;
	}

	public void setSubType(byte subType) {
		this.subType = subType;
	}

	public int getData_length() {
		return data_length;
	}

	public void setData_length(int data_length) {
		this.data_length = data_length;
	}
	

		
}
